package com.etime.shuhuiyuan.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.dto.CourseDto;
import com.etime.shuhuiyuan.pojo.Course;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.sql.Wrapper;

@Repository
public interface CourseDao extends BaseMapper<Course> {

    /**
     * 分页查询课程列表
     * @param page
     * @param wrapper
     * @return
     */
    @Select("select ct.coursetypename,c.courseid,c.coursetitle,c.coursestate,c.coursegoals," +
            " c.courseaudiences,c.coursepic,c.courseteachername,c.courseprice," +
            " c.courseoriginprice,c.coursecoinprice,c.coursecoinmaxpoint," +
            " c.coursecoinused,c.courserecommended,c.courseteachername,c.coursecommentcount" +
            " from course c join coursetype ct on c.coursetypeid=ct.coursetypeid")
    Page<CourseDto> selectCourseDtoByPage(Page page, QueryWrapper wrapper);

    /**
     * 根据courseid查询一条记录
     * @param courseid
     * @return
     */
    @Select("select ct.coursetypename,c.coursetypeid,c.courseid,c.coursetitle,c.coursestate,c.coursegoals," +
            " c.courseaudiences,c.coursepic,c.courseteachername,c.courseprice," +
            " c.courseoriginprice,c.coursecoinprice,c.coursecoinmaxpoint," +
            " c.coursecoinused,c.courserecommended,c.courseteachername,c.coursecommentcount" +
            " from course c join coursetype ct on c.coursetypeid=ct.coursetypeid" +
            " where c.courseid=#{courseid}")
    CourseDto selectCourseDtoById(Integer courseid);


    @Select({"<script>" +
            "select ct.coursetypename,c.courseid,c.coursetitle,c.coursestate,c.coursegoals," +
            " c.courseaudiences,c.coursepic,c.courseteachername,c.courseprice," +
            " c.courseoriginprice,c.coursecoinprice,c.coursecoinmaxpoint,"  +
            " c.coursecoinused,c.courserecommended,c.courseteachername,c.coursecommentcount" +
            " from course c join coursetype ct on c.coursetypeid=ct.coursetypeid" +
            "<where>"+
            "<if test='coursetitle!=null and coursetitle!=\"\"'>"+
            "and c.coursetitle like #{coursetitle}"+
            "</if>"+
            "<if test='coursestate!=null'>"+
            "and c.coursestate=#{coursestate}"+
            "</if>"+
            "</where>"+
            "</script>"
    })
    Page<CourseDto> selectCourseDtoByCon(CourseDto courseDto, Page page, @Param("coursetitle") String coursetitle, @Param("coursestate") Integer coursestate);

}
